# 使用OpenSUSE作为基础镜像
FROM opensuse/leap:latest

# 设置元数据
LABEL maintainer="wangkai<wkhuahuo@qq.com>"
LABEL description="OpenSUSE with Miniconda and JupyterBook for development"

# 设置环境变量
ENV MINICONDA_VERSION=latest \
    CONDA_DIR=/opt/conda \
    PATH=/opt/conda/bin:$PATH \
    WORKDIR=/workspace

# 安装系统依赖
RUN zypper -n refresh && \
    zypper -n update && \
    zypper -n install \
        wget \
        which \
        curl \
        git \
        gcc \
        gcc-c++ \
        make \
        python3 \
        libopenblas0 \
        libopenblas_serial0 \
        libgfortran5 \
        libstdc++6 \
        libgomp1 \
        libjpeg62 \
        libpng16-16 \
        libtiff5 \
        libopenmpi3 \
        libnuma1 \
        libhwloc5 \
        ocl-icd \
        ocl-icd-devel \
        shadow && \ 
    zypper clean -a

# 创建非root用户
RUN groupadd -r developer && \
    useradd -m -r -g developer developer && \
    mkdir -p /workspace && \
    chown developer:developer /workspace

# 切换工作目录
WORKDIR /workspace

# 安装Miniconda
RUN wget https://repo.anaconda.com/miniconda/Miniconda3-$MINICONDA_VERSION-Linux-x86_64.sh -O /tmp/miniconda.sh && \
    bash /tmp/miniconda.sh -b -p /opt/conda && \
    rm /tmp/miniconda.sh && \
    conda clean --all -y && \
    echo "conda activate base" >> /home/developer/.bashrc

# 配置Conda使用国内镜像（可选）
RUN conda config --set show_channel_urls yes && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ && \
    conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
   
RUN conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main && \
    conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r

# 安装JupyterBook及相关包


# RUN conda install -y -c conda-forge \
#         jupyter-book \
#         jupyter \
#         notebook \
#         matplotlib \
#         pandas \
#         numpy \
#         scipy && \
#     conda clean -afy

# 分步安装包
RUN pip install --no-cache-dir \
        --index-url https://pypi.tuna.tsinghua.edu.cn/simple \
        --trusted-host pypi.tuna.tsinghua.edu.cn \
        numpy scipy && \
    pip install --no-cache-dir \
        --index-url https://pypi.tuna.tsinghua.edu.cn/simple \
        --index-url https://pypi.tuna.tsinghua.edu.cn/simple \
        jupyter-book \
        jupyter \
        notebook \
        matplotlib \
        pandas 

# 安装 CPU 版本的 PyTorch
RUN pip install --no-cache-dir torch torchvision torchaudio



# 设置Jupyter配置以允许远程访问
RUN mkdir -p /home/developer/.jupyter && \
    chown -R developer:developer /home/developer/.jupyter

ENV PATH="/opt/conda/bin:${PATH}"
RUN jupyter notebook --generate-config --allow-root

RUN which jupyter && jupyter --version


# 设置容器健康检查
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
    CMD curl -f http://localhost:8888/api || exit 1

# 切换用户
USER developer

# 暴露Jupyter默认端口
EXPOSE 8888

# 设置默认工作目录
VOLUME ["/workspace"]
WORKDIR /workspace

# 设置容器启动命令
CMD ["jupyter", "notebook", \
     "--ip=0.0.0.0", \
     "--port=8888", \
     "--no-browser", \
     "--allow-root", \
     "--NotebookApp.token=''", \
     "--NotebookApp.password=''", \
     "--NotebookApp.allow_remote_access=True"]